/*
    日记模块相关api
*/
var express = require("express");
var router = express.Router();
const parseUrl = require('url');
var queryString = require("querystring")

let diary = require("../controller/diary")

const multer = require('multer');//图片上传中间件
var upload = multer({ dest: './public/uploads/' }) //上传图片储存路径
var type = upload.single('image')

//获取所有日记列表（仅获取日期和id）
router.get("/getDiaryList", (req, res) => {
    diary.getDiaryList().then(data => {
        res.send(JSON.stringify(data))
    })
})

//根据id获取日记详细信息
router.get("/getDiaryById", (req, res) => {
    let id = req.query.id
    diary.getDiary(id).then(data => {
        res.send(JSON.stringify(data))
    })
})

//添加日记
router.post("/addDiary", (req, res) => {
    let msg = req.body;
    diary.addDiary(msg).then(data => {
        res.send('ok')
    }).catch(err => {
        res.send(err)
    })
})

//修改日记
router.post("/update", (req, res) => {
    let msg = req.body;
    diary.updateDiary(msg).then(data => {
        res.send('ok')
    }).catch(err => {
        res.send(err)
    })
})

module.exports = router